package cn.stylefeng.guns.sys.modular.tao.service.serviceImpl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.tao.entity.GoodsPic;
import cn.stylefeng.guns.sys.modular.tao.mapper.GoodsPicMapper;
import cn.stylefeng.guns.sys.modular.tao.model.params.GoodsPicParam;
import cn.stylefeng.guns.sys.modular.tao.model.result.GoodsPicResult;
import  cn.stylefeng.guns.sys.modular.tao.service.GoodsPicService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 商品图片 服务实现类
 * </p>
 *
 * @author 
 * @since 2019-08-22
 */
@Service
public class GoodsPicServiceImpl extends ServiceImpl<GoodsPicMapper, GoodsPic> implements GoodsPicService {
    @Autowired
    GoodsPicMapper goodsPicMapper;
    @Override
    public void add(GoodsPicParam param){
        GoodsPic entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(GoodsPicParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(GoodsPicParam param){
        GoodsPic oldEntity = getOldEntity(param);
        GoodsPic newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public GoodsPicResult findBySpec(GoodsPicParam param){
        return null;
    }

    @Override
    public List<GoodsPicResult> findListBySpec(GoodsPicParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(GoodsPicParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public GoodsPic getByGoodsId(String goodsId) {
        return goodsPicMapper.getByGoodsId(goodsId);
    }

    @Override
    public void updateByGoodsId(GoodsPicParam goodsPicParam) {
        goodsPicMapper.updateByGoodsId(goodsPicParam);
    }


    /**
     * 修改图片
     */
    @Override
    public void updateGoodsPic(GoodsPicParam goodsPicParam) {
        goodsPicMapper.updateGoodsPic(goodsPicParam);
    }

    private Serializable getKey(GoodsPicParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private GoodsPic getOldEntity(GoodsPicParam param) {
        return this.getById(getKey(param));
    }

    private GoodsPic getEntity(GoodsPicParam param) {
        GoodsPic entity = new GoodsPic();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
